package com.cddx.logistics.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.cddx.logistics.domain.dto.plan.*;
import com.cddx.logistics.domain.pojo.plan.PlanCable2;
import com.cddx.logistics.domain.pojo.plan.PlanNew;
import com.cddx.logistics.domain.pojo.plan.PlanNormal;
import com.cddx.logistics.domain.pojo.plan.PlanSpare;
import com.cddx.logistics.service.PlanService;
import com.cddx.logistics.util.model.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/plan")
@Api(tags = "计划管理")
@RequiredArgsConstructor
public class PlanController {
    private final PlanService planService;
        /*
        普通计划的增删改查
        * */
    @PostMapping("/normal/list")
    @ApiOperation("普通计划分页查询")
    public R listNormal(PlanNormalPageDTO planNormalPageDTO){
        return planService.listNormalPage(planNormalPageDTO);
    }

    @DeleteMapping("/normal/delete")
    @ApiOperation("普通计划删除")
    public R deleteNormal(@RequestBody List<Integer> ids){
        return planService.deleteNormal(ids);
    }

    //删除单条数据
    @DeleteMapping("/normal/delete/{id}")
    @ApiOperation("普通计划删除")
    public R deleteNormal(@PathVariable Integer id){
        return planService.deleteNormal(id);
    }
    @PostMapping("/normal/add")
    @ApiOperation("普通计划添加")
    public R addNormal(@RequestBody PlanNormal planNormal){
        return planService.addNormal(planNormal);
    }
    @PostMapping("/normal/batch/add")
    @ApiOperation("普通计划批量导入")
    public R batchImportNormal(@RequestParam("file") MultipartFile file) {
        try {
            List<PlanNormal> planNormals = new ArrayList<>();
            EasyExcel.read(file.getInputStream(), PlanNormalExcelDTO.class, new PageReadListener<PlanNormalExcelDTO>(dataList -> {
                for (PlanNormalExcelDTO dto : dataList) {
                    PlanNormal planNormal = new PlanNormal();
                    BeanUtil.copyProperties(dto, planNormal);
                    String finishTime = dto.getFinishTime();
                    // 将字符串转换为日期
                     planNormal.setFinishTime(DateUtil.parse(finishTime));
                    planNormals.add(planNormal);
                }
            })).sheet().doRead();
            System.out.println("planNormals: " + planNormals);
            return planService.batchAddNormal(planNormals);
        } catch (Exception e) {
            return R.error("导入失败：" + e.getMessage());
        }
    }

    @PostMapping("/normal/update")
    @ApiOperation("普通计划修改")
    public R updateNormal(@RequestBody PlanNormal planNormal){

        return planService.updateNormal(planNormal);
    }

    /*
    * 备品计划的增删改查
    * */
    /*
     * 备品计划的增删改查
     */
    @PostMapping("/spare/list")
    @ApiOperation("备品计划分页查询")
    public R listSpare(PlanSparePageDTO planSparePageDTO) {
        return planService.listSparePage(planSparePageDTO);
    }

    @PostMapping("/spare/add")
    @ApiOperation("备品计划添加")
    public R addSpare(@RequestBody PlanSpare planSpare) {
        return planService.addSpare(planSpare);
    }

    @PostMapping("/spare/update")
    @ApiOperation("备品计划修改")
    public R updateSpare(@RequestBody PlanSpare planSpare) {
        return planService.updateSpare(planSpare);
    }

    @DeleteMapping("/spare/delete")
    @ApiOperation("备品计划批量删除")
    public R deleteSpare(@RequestBody List<Integer> ids) {
        return planService.deleteSpare(ids);
    }

    @DeleteMapping("/spare/delete/{id}")
    @ApiOperation("备品计划单条删除")
    public R deleteSpareById(@PathVariable Integer id) {
        return planService.deleteSpare(id);
    }
    /*
    新品临措计划
    * */
    /*
     * 新品临措计划的增删改查
     */
    @PostMapping("/new/list")
    @ApiOperation("新品临措计划分页查询")
    public R listNew(PlanNewPageDTO planNewPageDTO) {
        return planService.listNewPage(planNewPageDTO);
    }

    @PostMapping("/new/add")
    @ApiOperation("新品临措计划添加")
    public R addNew(@RequestBody PlanNew planNew) {
        return planService.addNew(planNew);
    }

    @PostMapping("/new/update")
    @ApiOperation("新品临措计划修改")
    public R updateNew(@RequestBody PlanNew planNew) {
        return planService.updateNew(planNew);
    }

    @DeleteMapping("/new/delete")
    @ApiOperation("新品临措计划批量删除")
    public R deleteNew(@RequestBody List<Integer> ids) {
        return planService.deleteNew(ids);
    }

    @DeleteMapping("/new/delete/{id}")
    @ApiOperation("新品临措计划单条删除")
    public R deleteNewById(@PathVariable Integer id) {
        return planService.deleteNew(id);
    }
/*
电缆2计划
* */
/*
 * 电缆2计划的增删改查
 */
@PostMapping("/cable2/list")
@ApiOperation("电缆2计划分页查询")
public R listCable2( PlanCable2PageDTO planCable2PageDTO) {
    return planService.listCable2Page(planCable2PageDTO);
}

    @PostMapping("/cable2/add")
    @ApiOperation("电缆2计划添加")
    public R addCable2(@RequestBody PlanCable2 planCable2) {
        return planService.addCable2(planCable2);
    }

    @PostMapping("/cable2/update")
    @ApiOperation("电缆2计划修改")
    public R updateCable2(@RequestBody PlanCable2 planCable2) {
        return planService.updateCable2(planCable2);
    }

    @DeleteMapping("/cable2/delete")
    @ApiOperation("电缆2计划批量删除")
    public R deleteCable2(@RequestBody List<Integer> ids) {
        return planService.deleteCable2(ids);
    }

    @DeleteMapping("/cable2/delete/{id}")
    @ApiOperation("电缆2计划单条删除")
    public R deleteCable2ById(@PathVariable Integer id) {
        return planService.deleteCable2(id);
    }


}
